﻿using Microsoft.Office.Interop.Word;
using Microsoft.Office.Tools.Ribbon;
using Microsoft.Office.Tools.Word;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WordAddIn
{
    public partial class Ribbon1
    {
        private Microsoft.Office.Interop.Word.Application app;
        private Microsoft.Office.Interop.Word.Document doc;
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {
            app = Globals.ThisAddIn.Application;
            doc = app.ActiveDocument;
          
            #region 控件展示选项卡展示用
            this.group5.DialogLauncherClick += DialogLauncher_func;
            this.dropDown1.ButtonClick += DropDownBtnClick;
            
            Debug.WriteLine(doc.FullName);            
            Debug.WriteLine(doc.Paragraphs.Count);
            Debug.WriteLine(doc.Tables.Count);
            Debug.WriteLine(doc.Shapes.Count+doc.InlineShapes.Count);
            #endregion


        }

        private void DropDownBtnClick(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show((sender as RibbonButton).Label);
        }


        #region button属性
        /*
         * 设计
         * Name 组件变量名 this.button1
         * 
         * 外观
         * Lable 按钮显示名称
         * Image 使用外部图标
         * OfficeImageId 使用Office自带的图标
         * ControlSize 控件尺寸 Regular（常规），`Large（大图标）
         * ScreenTip 提示文本
         * 
         * 行为
         * KeyTip 快捷键
         * Enable 是否启用控件
         * visible 是否显示控件
         */



        #endregion

        #region 控件展示选项卡 事件函数
        private void button2_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("点击按钮" + (sender as RibbonButton).Label);

        }

        private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
        {
            Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application;
            Microsoft.Office.Interop.Word.Document doc = app.ActiveDocument;
            
            if (this.toggleButton1.Checked)
            {
                doc.TrackRevisions = true;
                MessageBox.Show("切换按钮-选中");
            }
            else
            {
                doc.TrackRevisions = false;
                MessageBox.Show("切换按钮-取消");
            }
        }

        private void checkBox1_Click(object sender, RibbonControlEventArgs e)
        {
            if(this.checkBox1.Checked)
            {
                MessageBox.Show("复选框-选中");
            }
            else
            {
                MessageBox.Show("复选框-取消");
            }
        }

        private void comboBox1_TextChanged(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("组合框内容："+this.comboBox1.Text);
            
        }

        private void dropDown1_SelectionChanged(object sender, RibbonControlEventArgs e)
        {
            
            MessageBox.Show("下拉列表内容：" + this.dropDown1.SelectedItem.Label);
        }

        private void editBox1_TextChanged(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("编辑框内容："+this.editBox1.Text);
            this.label1.Label= this.editBox1.Text;
        }

        private void button3_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("点击菜单按钮1");
        }

        private void button4_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("点击菜单按钮2");

        }

        private void splitButton1_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("点击拆分按钮-主按钮");
        }

        private void button5_Click(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("点击"+(sender as RibbonButton).Label);
        }

        private void gallery1_Click(object sender, RibbonControlEventArgs e)
        {
            int index = this.gallery1.SelectedItemIndex;
            RibbonDropDownItem item = this.gallery1.Items[index];
            MessageBox.Show("点击："+item.Label);
            
        }
        
        private void DialogLauncher_func(object sender, RibbonControlEventArgs e)
        {
            MessageBox.Show("打开DialogLauncher");
        }
        #endregion

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {

            //app.Visible = false;            
            //Thread.Sleep(1000);
            //app.Visible = true;

            //app.WindowState = WdWindowState.wdWindowStateNormal;
            //app.Caption = "这是什么？";
            //doc.Content.Text = "这是什么？";
            try
            {
                Selection selection=app.Selection;
                if (selection.Type == WdSelectionType.wdSelectionNormal)
                {
                    selection.Font.Bold = 1;
                    selection.Font.Color = WdColor.wdColorRed;
                }else if (selection.Type == WdSelectionType.wdSelectionIP)
                {
                    selection.InsertAfter("vsto");
                    selection.Font.Bold = 1;
                    selection.Font.Italic = 1;
                    selection.Font.Color = WdColor.wdColorBlue;
                }
               
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message+"\n"+ex.StackTrace+"\n"+ex.InnerException);
            }

            
        }
    }
}
